package com.study.spmongodb.book.wwj.char26;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/3 20:56
 */
public class Worker extends Thread {

    private final ProductionChannel channel;

    private final static Random random = new Random(System.currentTimeMillis());

    public Worker(String workNmae, ProductionChannel productionChannel) {
        super(workNmae);
        this.channel = productionChannel;
    }

    @Override
    public void run() {
        while (true) {
            try {
                final Production production = channel.takeProduction();
                System.out.println(Thread.currentThread().getName() +
                        " process the" + production);
                production.create();

                TimeUnit.SECONDS.sleep(random.nextInt(10));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
